package com.rraj.common.redis.rediskey;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.rraj.common.redis.BaseRedisKey;
import com.rraj.common.redis.rediskey.HashRedisKeyField;
import com.xiaoleilu.hutool.util.CollectionUtil;

/**
 * Created by WangSheng on 2017/7/5.
 *
 * @author WangSheng
 * @date 2017/07/05
 */
public class HashRedisKey extends BaseRedisKey {
    private HashRedisKeyField field;
    private Collection<HashRedisKeyField> fields;

    public HashRedisKeyField getField() {
        return field;
    }

    public void setField(HashRedisKeyField field) {
        this.field = field;
    }

    public Collection<HashRedisKeyField> getFields() {
        return fields;
    }

    public void setFields(Collection<HashRedisKeyField> fields) {
        this.fields = fields;
    }

    /**
     * 获取hash的field的key组成的数组
     * @return
     */
    public String[] getHashFieldKeyArray(){
        if(CollectionUtil.isNotEmpty(fields)){
            String[] keyArr = new String[fields.size()];
            int index = 0;
            for(HashRedisKeyField field : fields){
                keyArr[index] = field.getDataBaseFieldKey();
                index ++;
            }
            return keyArr;
        }else{
            return new String[]{field.getDataBaseFieldKey()};
        }
    }

    /**
     * 获取批量操作的field的map集合
     * @return
     */
    public Map<String,String> getHashFieldMap() {
        Map<String, String> map = new HashMap<>();
        if(CollectionUtil.isNotEmpty(fields)){
            for(HashRedisKeyField field : fields){
                map.put(field.getDataBaseFieldKey(), field.getValue());
            }
        }else{
            map.put(field.getDataBaseFieldKey(), field.getValue());
        }
        return map;
    }
}
